import { computed, reactive, Ref, ref } from "vue";
import CommonHook from "@/hook/commonHook";
export default function InitHook(state: { passData: Ref<DetainGoods.ListRows[]> }) {
  const { passData } = state;
  const formDataRef = ref<any>(null);
  const loading = ref<boolean>(false);
  const { dataDict } = CommonHook();
  const formState = reactive<DetainGoods.ApplyReleaseCargo>({
    customerId: "",
    waybillIdList: [],
    applyType: "1",
    delayDate: "",
    applyRemark: "",
    bizFileDetailReqDtoList: []
  });
  const amount = computed<string>(() => {
    const allBillNo = passData.value.map((t: DetainGoods.ListRows) => t.overdueBillNo);
    const result = Array.from(new Set(allBillNo));
    const allAmount = [];
    for (let i = 0; i < result.length; i++) {
      const target = passData.value.find((t: DetainGoods.ListRows) => t.overdueBillNo === result[i]) || { overdueAmount: 0 };
      allAmount.push(target.overdueAmount || 0);
    }
    return allAmount.reduce((a: number, b: number) => (a * 100 + b * 100) / 100, 0).toFixed(2);
  });
  const billNo = computed<string>(() => {
    const allBillNo = passData.value.map((t: DetainGoods.ListRows) => t.overdueBillNo);
    const result = Array.from(new Set(allBillNo));
    return result.join(",");
  });
  return {
    formState,
    loading,
    formDataRef,
    dataDict,
    amount,
    billNo
  };
}
